package com.offcn.sellergoods.controller;

import com.offcn.sellergoods.gruop.GoodsGroup;
import com.offcn.sellergoods.pojo.Goods;
import com.offcn.sellergoods.service.GoodsService;
import com.offcn.common.entity.PageResult;
import com.offcn.common.entity.Result;
import com.offcn.common.entity.StatusCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.el.StandardELContext;
import java.util.List;

/**
 * 商品管理
 *
 * @Author:ujiuye
 * @Description:
 * @Date 2021/2/1 14:19
 *****/
@Api(tags = "GoodsController")
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * Goods分页条件搜索实现
     *
     * @param goods
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Goods条件分页查询", notes = "分页条件查询Goods方法详情", tags = {"GoodsController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageResult<Goods>> findPage(@RequestBody(required = false) @ApiParam(name = "Goods对象", value = "传入JSON数据", required = false) Goods goods, @PathVariable int page, @PathVariable int size) {
        //调用GoodsService实现分页条件查询Goods
        PageResult<Goods> pageResult = goodsService.findPage(goods, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * Goods分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Goods分页查询", notes = "分页查询Goods方法详情", tags = {"GoodsController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageResult<Goods>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用GoodsService实现分页查询Goods
        PageResult<Goods> pageResult = goodsService.findPage(page, size);
        return new Result<PageResult<Goods>>(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param goods
     * @return
     */
    @ApiOperation(value = "Goods条件查询", notes = "条件查询Goods方法详情", tags = {"GoodsController"})
    @PostMapping(value = "/search")
    public Result<List<Goods>> findList(@RequestBody(required = false) @ApiParam(name = "Goods对象", value = "传入JSON数据", required = false) Goods goods) {
        //调用GoodsService实现条件查询Goods
        List<Goods> list = goodsService.findList(goods);
        return new Result<List<Goods>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "Goods根据ID删除", notes = "根据ID删除Goods方法详情", tags = {"GoodsController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Long id) {
        try {
            //调用GoodsService实现根据主键删除
            goodsService.delete(id);
            return new Result(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "删除失败，商品目前处于上架状态中，请先下架在进行删除!");
        }
    }

    /**
     * 修改Goods数据
     *
     * @param goodsGroup
     * @param id
     * @return
     */
    @ApiOperation(value = "Goods根据ID修改", notes = "根据ID修改Goods方法详情", tags = {"GoodsController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody @ApiParam(name = "Goods对象", value = "传入JSON数据", required = false) GoodsGroup goodsGroup, @PathVariable Long id) {
        //设置主键值
        goodsGroup.getGoods().setId(id);
        //调用GoodsService实现修改Goods
        goodsService.update(goodsGroup);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 新增Goods数据
     *
     * @param goodsGroup
     * @return
     */
    @ApiOperation(value = "Goods添加", notes = "添加Goods方法详情", tags = {"GoodsController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Goods对象", value = "传入JSON数据", required = true) GoodsGroup goodsGroup) {
        //调用GoodsService实现添加Goods
        goodsService.add(goodsGroup);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 根据ID查询Goods数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "Goods根据ID查询", notes = "根据ID查询Goods方法详情", tags = {"GoodsController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Long")
    @GetMapping("/{id}")
    public Result<GoodsGroup> findById(@PathVariable(required = false) Long id) {
        //调用GoodsService实现根据主键查询Goods
        GoodsGroup goodsGroup = goodsService.findById(id);
        return new Result<GoodsGroup>(true, StatusCode.OK, "查询成功", goodsGroup);
    }

    /**
     * 查询Goods全部数据
     *
     * @return
     */
    @ApiOperation(value = "查询所有Goods", notes = "查询所Goods有方法详情", tags = {"GoodsController"})
    @GetMapping
    public Result<List<Goods>> findAll() {
        //调用GoodsService实现查询所有Goods
        List<Goods> list = goodsService.findAll();
        return new Result<List<Goods>>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 商品审核操作
     *
     * @return
     */
    @ApiOperation(value = "商品审核操作", notes = "商品审核操作详情", tags = {"GoodsController"})
    @PutMapping("audit")
    public Result audit(Long id, String status) {
        //调用GoodsService实现查询所有Goods
        goodsService.audit(id, status);
        return new Result(true, StatusCode.OK, "操作成功");
    }

    /**
     * 商品上架
     *
     * @return
     */
    @ApiOperation(value = "商品上架", notes = "商品上架详情", tags = {"GoodsController"})
    @PutMapping("upMarkble")
    public Result upMarkble(Long id) {
        try {
            //调用GoodsService实现查询所有Goods
            goodsService.upMarkble(id);
            return new Result(true, StatusCode.OK, "上架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "上架失败,请检查商品是否存在或审核是否通过");
        }
    }

    /**
     * 商品下架
     *
     * @return
     */
    @ApiOperation(value = "商品下架", notes = "商品下架详情", tags = {"GoodsController"})
    @PutMapping("lowerMarkble")
    public Result lowerMarkble(Long id) {
        try {
            //调用GoodsService实现查询所有Goods
            goodsService.lowerMarkble(id);
            return new Result(true, StatusCode.OK, "下架成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "请检查商品是否存");
        }
    }

    /**
     * 商品还原
     *
     * @return
     */
    @ApiOperation(value = "商品还原", notes = "商品还原详情", tags = {"GoodsController"})
    @PutMapping("reductionMarkble")
    public Result reductionMarkble(Long id) {
        try {
            //调用GoodsService实现查询所有Goods
            goodsService.reductionMarkble(id);
            return new Result(true, StatusCode.OK, "还原成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(false, StatusCode.ERROR, "请检查商品是否存");
        }
    }

}
